package com.wideplay.crosstalk.services;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * Ensures that only users who are logged in properly are allowed to use the site.
 *
 * @author Dhanji R. Prasanna (dhanji@gmail.com)
 */
class SecurityInterceptor implements MethodInterceptor {
    private final AuthenticationManager authenticationManager;

    public SecurityInterceptor(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {

        //proceed with normal execution if a user is logged in
        if (authenticationManager.isLoggedIn()) {
            return methodInvocation.proceed();
        }

        //redirect to the login page if the user is not properly logged in!
        return "/login";
    }
}
